package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Signin;
import cn.wolfcode.crm.query.SigninQueryObject;
import cn.wolfcode.crm.service.ISigninService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("signin")
public class SigninController {
    @Autowired
    private ISigninService service;

    @RequestMapping("")
    public String index(){
        return "signin/list";
    }

    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("signin:data")
    @PermissionName("签到表数据")
    public PageResult list(SigninQueryObject qo){

        return service.query(qo);
    }

    @RequestMapping("queryDepts")
    @ResponseBody
    public List<Signin> queryDepts(){
        return service.list();
    }

    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("signin:save")
    @PermissionName("签到")
    public JsonResult save(Signin signin){
        JsonResult result = new JsonResult();
        try {
            service.save(signin);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(e.getMessage());
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("signin:update")
    @PermissionName("签退")
    public JsonResult update(Signin signin){
        JsonResult result = new JsonResult();
        try {
            service.update(signin);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(e.getMessage());
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions("signin:delete")
    @PermissionName("签到数据删除")
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            service.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(e.getMessage());
        }
        return result;
    }
}
